﻿
// TypingDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Typing.h"
#include "TypingDlg.h"
#include "afxdialogex.h"
#include "LanGameDlg.h"
#include <mmsystem.h>

using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 引用 Windows Multimedia API
#pragma comment(lib,"winmm.lib")
#pragma comment(lib, "lib\\HPSocket_UD.lib")


// CTypingDlg 对话框

//判断点在两点确定的直线上,使用直线方程(y-y1)(x2-x1)==(x-x1)(y2-y1)判断
bool isPointInLine(CPoint& pt, CPoint& pt1, CPoint& pt2) {
	double delta = ((double)pt.y - pt1.y) * ((double)pt2.x - pt1.x) - ((double)pt.x - pt1.x) * ((double)pt2.y - pt1.y);
	return abs(delta) < 1e-5 ? true : false;
}

ServerHandler sh;
ClientHandler ch;
SearchHandler seh;


CTypingDlg::CTypingDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTypingDlg::IDD, pParent)
	, m_strTyping(_T(""))
	, m_nTyping(0)
	, m_nPngFrame(0)
	, m_nPngEnemyHeight(48)
	, m_nPngRoleHeight(48)
	, m_nPngEnemyWidth(48)
	, m_nPngRoleWidth(48)
	, m_tcpServer(&sh)
	, m_tcpClient(&ch)
{
	clientConId = -1;
	m_roleCircles = 0;
	m_enemyCircles = 0;
	sh.pDlg = this;
	ch.pDlg = this;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_searchClients = new CTcpClientPtr*[254];
	memset(m_searchClients, 0, sizeof(CTcpClientPtr*) * 254);
	for (int i = 0; i < 254; i++) {
		m_searchClients[i] = new CTcpClientPtr(&seh);
	}
}

CTypingDlg::~CTypingDlg()
{
	for (int i = 1; i <= 254; i++) {
		CTcpClientPtr& client = *m_searchClients[i - 1];
		SearchStruct* extra = (SearchStruct*)client->GetExtra();
		if (extra != nullptr) {
			delete extra;
			extra = nullptr;
		}
		delete m_searchClients[i - 1];
	}
	delete[] m_searchClients;
}

void CTypingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PIC_MAP, m_picMap);
	DDX_Control(pDX, IDC_STATIC_INPUT, m_picInput);
}

BEGIN_MESSAGE_MAP(CTypingDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	//ON_BN_CLICKED(IDC_BUTTON1, &CTypingDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BTN_GETART, &CTypingDlg::OnBnClickedBtnGetart)
	ON_WM_CHAR()
	ON_WM_ERASEBKGND()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_CON, &CTypingDlg::OnBnClickedButtonCon)
END_MESSAGE_MAP()


// CTypingDlg 消息处理程序

BOOL CTypingDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	GetDlgItem(IDC_STATIC_INPUT)->SetFocus();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	InitValues();
	m_imgMap.Load(_T("img/maps.jpg"));
	m_imgRole.Load(_T("img/player.png"));
	m_imgEnemy.Load(_T("img/pc.png"));
	m_imgStart.Load(_T("img/Meadow.png"));
	pngTransparent(m_imgRole);
	pngTransparent(m_imgEnemy);

	//通用的定时器
	SetTimer(1, 100, NULL);
	//行走图动画专用定时器
	SetTimer(2, 200, NULL);

	CWnd* pbtn=GetDlgItem(IDC_BTN_GETART);
	pbtn->EnableWindow(false);

	mciSendString(L"open mp3\\bgsong.mp3 alias mymusic", NULL, 0, NULL);
	mciSendString(L"play mymusic", NULL, 0, NULL);
	//启动
	OnBnClickedBtnGetart();
	GetHostAddress(m_hostIp);
	return FALSE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTypingDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
		
	}
	else
	{
		CDialogEx::OnPaint();
		//绘制地图
		CDC* pdcMap = m_picMap.GetDC();
		CRect rc;
		m_picMap.GetClientRect(rc);

		CImage memImg;
		if (m_gameMode == STARTPAGE) {
			ImageCopy(&m_imgStart, &memImg);
		}
		else {
			ImageCopy(&m_imgMap, &memImg);
		}
		CDC memDC;
		memDC.CreateCompatibleDC(pdcMap);
		memDC.SelectObject(CBitmap::FromHandle(memImg));

		::SetStretchBltMode(memDC.m_hDC, STRETCH_HALFTONE); //设置拉伸方式，避免缩小时失真

		//绘制人物
		CPoint& rolePt = GetXY(m_nRoleSteps, m_roleStart, 1);
		CPoint& enemyPt = GetXY(m_nEnemySteps, m_enemyStart, 2);
		DrawPlayer(1, rolePt, &memDC);
		DrawPlayer(2, enemyPt, &memDC);
		//碰撞检测
		if (m_gameMode != GAMEOVER && m_roleCircles == m_enemyCircles) {
			if (abs(rolePt.x - enemyPt.x) + abs(rolePt.y - enemyPt.y) < 48) {
				m_gameMode = GAMEOVER;
				MessageBox(_T("警察胜利"), _T("游戏结束"), MB_OK);
				InitValues();
				m_gameMode = STARTPAGE;

			}
		}
		else if (m_gameMode != GAMEOVER && m_roleCircles < m_enemyCircles) {
			if (abs(rolePt.x - enemyPt.x) + abs(rolePt.y - enemyPt.y) < 48) {
				m_gameMode = GAMEOVER;
				MessageBox(_T("小偷胜利"), _T("游戏结束"), MB_OK);
				InitValues();
				m_gameMode = STARTPAGE;
			}
		}


		pdcMap->StretchBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, 962, 414, SRCCOPY);

		ReleaseDC(pdcMap);
		//
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTypingDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CTypingDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}


BOOL CTypingDlg::PreTranslateMessage(MSG* pMsg)
{
	// 消息预处理
	if (pMsg->message == WM_KEYDOWN)
	{
		char c = pMsg->wParam;
		if (m_gameMode == STARTPAGE)
		{
			if (c == VK_RETURN) {
				m_gameMode = SINGLE;
				return true;
			}
			else {
				return true;
			}
		}
		else if (c == VK_RETURN)
		{
			return true;
		}
	}
	else if(pMsg->message == WM_CHAR)
	{
		char c = pMsg->wParam;
		if ((c >= '0'&&c <= '9') || (c >= 'a'&&c <= 'z') 
			|| (c >= 'A'&&c <= 'Z') || c == ' ' 
			|| c == ',' || c == '\'' || c == '"' 
			|| c == '!' || c == '.')
		{
			if (m_gameMode == LAN_CLIENT) {
				if (m_nTyping < m_strOneLine.GetLength())
				{
					CString s(c);
					if (!InputRight(c)) {
						//输入错误，警告音
						return true;
					}
					m_strTyping += s;
					m_nTyping++;
					m_nEnemySteps += m_nEnemySpeed;

					char str[64];
					sprintf(str, "typing:client-steps:%f\r\n\0", m_nEnemySteps);
					BYTE buf[64];
					memcpy(buf, str, strlen(str));
					m_tcpClient->Send(buf, strlen(str));

					if (m_nTyping == m_strOneLine.GetLength())
					{
						if (!GetNowLine(m_nArtIndex + 1)) {
							MessageBox(_T("游戏结束，时间到，追赶者失败"));
						}
						m_nTyping = 0;
						m_strTyping = _T("");
					}
					return true;
				}
			}
			else {
				if (m_nTyping < m_strOneLine.GetLength())
				{
					CString s(c);
					if (!InputRight(c)) {
						//输入错误，警告音
						return true;
					}
					m_strTyping += s;
					m_nTyping++;
					m_nRoleSteps += m_nRoleSpeed;
					//发送网络对战信息
					if (m_gameMode == LAN_SERVER) {
						if (clientConId != -1) {
							char str[64];
							sprintf(str, "typing:server-steps:%f\r\n\0", m_nRoleSteps);
							BYTE buf[64];
							memcpy(buf, str, strlen(str));
							m_tcpServer->Send(clientConId, buf, strlen(str));
						}
					}
					if (m_nTyping == m_strOneLine.GetLength())
					{
						if (!GetNowLine(m_nArtIndex + 1)) {
							MessageBox(_T("游戏结束，时间到，追赶者失败"));
						}
						m_nTyping = 0;
						m_strTyping = _T("");
					}
					return true;
				}
			}
		}
		return true;
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}

void CTypingDlg::DrawTextArea(CDC* p)
{
	CRect rc;
	m_picMap.GetClientRect(rc);

	if (m_nTyping == m_strOneLine.GetLength()) {
		p->FillRect(&rc, &CBrush(RGB(128, 128, 192)));
	}
	p->TextOutW(24, 16, m_strOneLine, m_strOneLine.GetLength());
	p->SetTextColor(RGB(255, 0, 0));
	p->TextOut(24, 16, m_strTyping, m_nTyping);
	p->SetTextColor(RGB(0, 0, 0));
}

void CTypingDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == 1) {
		//文字窗口
		CDC* p = m_picInput.GetDC();

		DrawTextArea(p);
		ReleaseDC(p);

		if (m_gameMode == SINGLE) {
			//PC 自动行走
			m_nEnemySteps += m_nEnemySpeed;
		}

		InvalidateRect(getPlayerRect(1, m_nRoleSteps - m_nRoleSpeed), true);
		InvalidateRect(getPlayerRect(2, (int)(m_nEnemySteps - m_nEnemySpeed)), true);
	}
	else if (nIDEvent == 2) {
		//行走图动画
		m_nPngFrame = m_nPngFrame >= 2 ? 0 : m_nPngFrame + 1;
	}

	CDialogEx::OnTimer(nIDEvent);
}

void CTypingDlg::OnBnClickedBtnGetart()
{
	//读取当前文章
	FILE* p=fopen("art1.txt","rb");
	fseek(p,0,SEEK_END);
	int max=ftell(p);
	fseek(p,0,SEEK_SET);
	char* c=new char[max+1];
	memset(c, 0, max);

	fread(c,1,max,p);
	fclose(p);
	m_strArt=CString(c);
	m_strArt.Remove('\r');

	delete[] c;
	PreArticle();
	GetNowLine(0);
}


// 处理m_strArt，消除空行，清除非法字符,保持单词之间空格
void CTypingDlg::PreArticle(void)
{
	CString tem=m_strArt;
	for(int i=0;i<tem.GetLength();i++){
		char c=tem[i];
		if((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||c==' '||c==','||c=='\''||c=='"'||c=='!'||c=='.')
		{
		}
		else{
			tem.SetAt(i,' ');
		}
		if(i>0&&tem[i]==' '&&tem[i-1]==' ')
		{
			int sss=tem.GetLength();
			tem.Delete(i);
				i--;
		}
	}
	m_strArt=tem;
}


// 获得当前要输入参照文章一行，保证最后一个单词完整，
//如果有标点，标点在最后最后一个字符不是空格
int CTypingDlg::GetNowLine(int index)
{
	if (index >= m_strArt.GetLength()) {
		return 0;
	}
	m_strOneLine=m_strArt.Mid(index,MAX_CACHE);
	if(m_strOneLine.GetLength()<MAX_CACHE)
	{
		if(m_strOneLine[0]==' ')
		{
			m_nArtIndex++;
		m_strOneLine.Delete(0,1);
		}
		m_nArtIndex=m_strArt.GetLength()-1;
			return m_strOneLine.GetLength();
	}
	if(m_strArt[m_strOneLine.GetLength()+index]!=' ')
	{
		
		int i=m_strOneLine.GetLength()-1;
		if(m_strOneLine[i]==' ')
		{
			m_strOneLine=m_strOneLine.Left(i);
		}
		else
		{
			while(m_strOneLine[i]!=' ')i--;
			m_strOneLine=m_strOneLine.Left(i);
		}
	}
	if(m_strOneLine[0]==' ')
		{
			m_nArtIndex++;
		m_strOneLine.Delete(0,1);
		}
	m_nArtIndex+=m_strOneLine.GetLength();
	return m_strOneLine.GetLength();
}

void CTypingDlg::searchLanGame()
{
	m_foundHost.clear();
	if (m_hostIp.GetLength() > 0) {
		int idx = m_hostIp.ReverseFind('.');
		CString prefix = m_hostIp.Left(idx);

		for (int i = 1; i <= 254; i++) {
			CString newIp;
			newIp.Format(prefix + ".%d", i);
			if (newIp.Compare(m_hostIp) == 0) {
				continue;
			}
			CTcpClientPtr& client = *m_searchClients[i - 1];

			SearchStruct* extra = (SearchStruct*)client->GetExtra();
			if (extra != nullptr) {
				delete extra;
				extra = nullptr;
			}

			SearchStruct* pStruct = new SearchStruct;
			pStruct->pMain = this;
			pStruct->host = newIp;

			client->SetExtra(pStruct);
			client->Start(newIp, SERVER_PORT);
		}
	}
}

void CTypingDlg::CreateLanGame()
{
	if (m_tcpServer->GetState() != SS_STOPPED) {
		m_tcpServer->Stop();
	}
	m_tcpServer->Start(SERVER_ADDRESS, SERVER_PORT);
	MessageBox(_T("创建主机"), _T("网络对战"), MB_OK);
}

void CTypingDlg::JoinLanGame(CString & address, int & port)
{
	if (m_tcpClient->GetState() != SS_STOPPED) {
		m_tcpClient->Stop();
	}
	m_tcpClient->Start(address, port);
}

void CTypingDlg::handleJoin(CONNID conid)
{
	int result = MessageBox(_T("是否接受其他用户的挑战?"), _T("系统提示"), MB_OKCANCEL);
	if (result == IDOK) {
		m_gameMode = CGameMode::LAN_SERVER;
		m_nEnemySpeed = 6;
		m_nRoleSpeed = 6;

		char* str = "typing:accept\r\n\0";
		BYTE buf[64];
		memcpy(buf, str, strlen(str));
		m_tcpServer->Send(conid, buf, strlen(str));
		clientConId = conid;
	}
	else {
		char* str = "typing:decline\r\n\0";
		BYTE buf[64];
		memcpy(buf, str, strlen(str));
		m_tcpServer->Send(conid, buf, strlen(str));
	}
}

void CTypingDlg::pngTransparent(CImage & img)
{
	if (img.GetBPP() == 32) {
		int i; int j; 
		for (i = 0; i < img.GetWidth(); i++) {
			for (j = 0; j < img.GetHeight(); j++) {
				byte *pByte = (byte *)img.GetPixelAddress(i, j);
				pByte[0] = pByte[0] * pByte[3] / 255;
				pByte[1] = pByte[1] * pByte[3] / 255;
				pByte[2] = pByte[2] * pByte[3] / 255;
		}
	}
}
}


bool CTypingDlg::InputRight(char c)
{
	//当前已经输入了m_nTyping个正确的,下一个的下标是m_nTyping
	if(m_strOneLine[m_nTyping]==c){
		return true;
	}
	return false;
}


void CTypingDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	//
	CDialogEx::OnChar(nChar, nRepCnt, nFlags);
}


void CTypingDlg::InitValues(void)
{
	m_nTyping = 0;
	m_nArtIndex = 0;
	m_nEnemySteps = 0;
	m_nRoleSteps = 0;
	//初始化路径数组
	m_routes.push_back(CPoint(140, 242));
	m_routes.push_back(CPoint(680, 242));
	m_routes.push_back(CPoint(680, 50));
	m_routes.push_back(CPoint(877, 50));
	m_routes.push_back(CPoint(877, 335));
	m_routes.push_back(CPoint(10, 335));
	m_routes.push_back(CPoint(10, 50));
	m_routes.push_back(CPoint(140, 50));
	//初始化起点
	m_roleStart = CPoint(234, 242);
	m_enemyStart = CPoint(600, 242);
	m_nEnemySpeed = 2;
	m_nRoleSpeed = 6;
	//初始化图像
	m_roleInPng.x = 144;
	m_roleInPng.y = 0;
	m_roleFaceTo = 2;
	m_enemyInPng.x = 144;
	m_enemyInPng.y = 0;
	m_enemyFaceTo = 2;
}

//复制CImage
void CTypingDlg::ImageCopy(CImage* pImgSrc, CImage* pImgDrt)
{
	int MaxColors = pImgSrc->GetMaxColorTableEntries();
	RGBQUAD* ColorTab;
	ColorTab = new RGBQUAD[MaxColors];

	CDC *pDCsrc, *pDCdrc;
	if (!pImgDrt->IsNull())
	{
		pImgDrt->Destroy();
	}
	pImgDrt->Create(pImgSrc->GetWidth(), pImgSrc->GetHeight(), pImgSrc->GetBPP(), 0);

	if (pImgSrc->IsIndexed())
	{
		pImgSrc->GetColorTable(0, MaxColors, ColorTab);
		pImgDrt->SetColorTable(0, MaxColors, ColorTab);
	}

	pDCsrc = CDC::FromHandle(pImgSrc->GetDC());
	pDCdrc = CDC::FromHandle(pImgDrt->GetDC());
	pDCdrc->BitBlt(0, 0, pImgSrc->GetWidth(), pImgSrc->GetHeight(), pDCsrc, 0, 0, SRCCOPY);
	pImgSrc->ReleaseDC();
	pImgDrt->ReleaseDC();
	delete ColorTab;
}

CRect CTypingDlg::getPlayerRect(int pid, int steps)
{
	if (pid == 1) {
		CPoint pt = GetXY(steps, m_roleStart, 1);
		return CRect(pt.x, pt.y, pt.x + 48, pt.y + 48);
	}
	else {
		CPoint pt = GetXY(steps, m_enemyStart, 2);
		return CRect(pt.x, pt.y, pt.x + 48, pt.y + 48);
	}
}

//绘制角色,pid:playerid
void CTypingDlg::DrawPlayer(int pid, CPoint pt, CDC* p)
{
	if (pid == 1) {
		m_imgRole.Draw(p->m_hDC, pt.x - m_nPngRoleWidth / 2, pt.y - m_nPngRoleHeight,
			m_nPngRoleWidth, m_nPngRoleHeight, m_roleInPng.x + m_nPngFrame * m_nPngRoleWidth,
			m_roleInPng.y + m_roleFaceTo * m_nPngRoleHeight, m_nPngRoleWidth, m_nPngRoleHeight);
	}
	else {
		m_imgEnemy.Draw(p->m_hDC, pt.x - m_nPngEnemyWidth / 2, pt.y - m_nPngEnemyHeight,
			m_nPngEnemyWidth, m_nPngEnemyHeight, m_enemyInPng.x + m_nPngFrame * m_nPngEnemyWidth,
			m_enemyInPng.y + m_enemyFaceTo * m_nPngEnemyHeight, m_nPngEnemyWidth, m_nPngEnemyHeight);
	}
}

//获取角色的绘制位置
CPoint CTypingDlg::GetXY(const int nStep, CPoint& start, int pid)
{
	//从起点开始加上步数
	CPoint pt(start);//返回值
	int count = 0;
	bool isGotoStart = false;
	//轨迹横平竖直必然落到某条线段上
	CPoint* pstart = &m_routes[0];
	CPoint* pend = &m_routes[1];
	int iStart = 0;
	int iEnd = 1;

	int& nFaceTo = pid == 1 ? m_roleFaceTo : m_enemyFaceTo;

	int nCircle = 0;

	while (count < nStep) {
		if (pstart->x == pend->x) {
			//垂直方向
			if (pstart->y < pend->y) {
				nFaceTo = 0;//向下走
				if (nStep - count < pend->y - pt.y) {
					pt.y += nStep - count;
					count += nStep - count;
					break;
				}
				else {
					count += pend->y - pt.y;
					pt.y = pend->y;
					iStart = iEnd;
					iEnd = iEnd + 1 < m_routes.size() ? iEnd + 1 : 0;
					pstart = &m_routes[iStart];
					pend = &m_routes[iEnd];
					if (iStart == 0 && iEnd == 1) {
						nCircle++;
					}
				}
			}
			else {
				nFaceTo = 3;//向上走
				if (nStep - count < pt.y - pend->y) {
					pt.y -= nStep - count;
					count += nStep - count;
					break;
				}
				else {
					count += pt.y - pend->y;
					pt.y = pend->y;
					iStart = iEnd;
					iEnd = iEnd + 1 < m_routes.size() ? iEnd + 1 : 0;
					pstart = &m_routes[iStart];
					pend = &m_routes[iEnd];
					if (iStart == 0 && iEnd == 1) {
						nCircle++;
					}
				}
			}
		}
		else if (pstart->y == pend->y) {
			//水平方向
			if (pstart->x < pend->x) {
				nFaceTo = 2;//向右走
				if (nStep - count < pend->x - pt.x) {
					pt.x += nStep - count;
					count += nStep - count;
					break;
				}
				else {
					count += pend->x - pt.x;
					pt.x = pend->x;
					iStart = iEnd;
					iEnd = iEnd + 1 < m_routes.size() ? iEnd + 1 : 0;
					pstart = &m_routes[iStart];
					pend = &m_routes[iEnd];
					if (iStart == 0 && iEnd == 1) {
						nCircle++;
					}
				}
			}
			else {
				nFaceTo = 1;//向左走
				if (nStep - count < pt.x - pend->x) {
					pt.x -= nStep - count;
					count += nStep - count;
					break;
				}
				else {
					count += pt.x - pend->x;
					pt.x = pend->x;
					iStart = iEnd;
					iEnd = iEnd + 1 < m_routes.size() ? iEnd + 1 : 0;
					pstart = &m_routes[iStart];
					pend = &m_routes[iEnd];
					if (iStart == 0 && iEnd == 1) {
						nCircle++;
					}
				}
			}
		}
	}

	if (pid == 1) {
		m_roleCircles = nCircle;
	}
	else {
		m_enemyCircles = nCircle;
	}
	return pt;
}


BOOL CTypingDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	return true;
	//return CDialogEx::OnEraseBkgnd(pDC);
}


void CTypingDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	mciSendString(L"stop mymusic", NULL, 0, NULL);
	mciSendString(L"close mymusic", NULL, 0, NULL);
	CDialogEx::OnClose();
}

void CTypingDlg::GetHostAddress(CString &strIPAddr)
{
	char    HostName[100];
	gethostname(HostName, sizeof(HostName));// 获得本机主机名.

	hostent* hn = nullptr;
	hn = gethostbyname(HostName);//根据本机主机名得到本机ip
	if (hn != nullptr) {
		for (int i = 0; hn->h_addr_list[i] != nullptr; i++) {
			CString temp = CString(inet_ntoa(*(struct in_addr *)hn->h_addr_list[i]));
			//假设有虚拟网卡，只选取192.168开头的
			if (temp.Left(7).Compare(_T("192.168")) == 0) {
				strIPAddr = temp;
				break;
			}
		}
	}
}

//打开联机对话框
void CTypingDlg::OnBnClickedButtonCon()
{
	CLanGameDlg dlg;
	dlg.m_parentDlg = this;
	INT_PTR nResponse = dlg.DoModal();
	GetDlgItem(IDC_STATIC_INPUT)->SetFocus();
}
